package com.serita.yuyin;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;

/**
 * Created by 12985 on 2016/12/1.
 */
public class PermisssionUtils {

    public static  final  int REQUEST_CODE_WRITE = 101;
    public static  final  int REQUEST_CODE_CAMERA= 102;
    public static  final  int REQUEST_CODE_RED = 103;
    public static boolean isHasFileWritePermission(Activity activity){
        if (Build.VERSION.SDK_INT >= 23) {
            String[] permissions = {android.Manifest.permission.WRITE_EXTERNAL_STORAGE};
            for(String str:permissions) {
                if (activity.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
                    activity.requestPermissions(permissions,REQUEST_CODE_WRITE);
                    return false;
                }
            }
        }
        return true;
    }
    public static boolean isHasFileReadPermission(Activity activity){
        if (Build.VERSION.SDK_INT >= 23) {
            String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE};
            for(String str:permissions) {
                if (activity.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
                    activity.requestPermissions(permissions,REQUEST_CODE_RED);
                    return false;
                }
            }
        }
        return true;
    }


    public static boolean isHasCameraPermission(Activity activity){
        if (Build.VERSION.SDK_INT >= 23) {
            String[] permissions = {android.Manifest.permission.CAMERA};
            for(String str:permissions) {
                if (activity.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
                    activity.requestPermissions(permissions,REQUEST_CODE_CAMERA);
                    return false;
                }
            }
        }
        return true;
    }
    public static boolean isHasRecordAudioPermission(Activity activity){
        if (Build.VERSION.SDK_INT >= 23) {
            String[] permissions = {android.Manifest.permission.RECORD_AUDIO};
            for(String str:permissions) {
                if (activity.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
                    activity.requestPermissions(permissions,REQUEST_CODE_CAMERA);
                    return false;
                }
            }
        }
        return true;
    }
    public static boolean isHasLocationPermission(Activity activity){
        if (Build.VERSION.SDK_INT >= 23) {
            String[] permissions = {android.Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
            for(String str:permissions) {
                if (activity.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
                    activity.requestPermissions(permissions,REQUEST_CODE_CAMERA);
                    return false;
                }
            }
        }
        return true;
    }
}
